<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class App_model extends CI_Model {

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
                $this->load->database();
	}

	function list_app_user($userid){
		$this->load->database();
		$this->db->order_by("id", "desc");
		$this->db->where('user_id',$userid);
		$query=$this->db->get('tbl_app');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function return_app_name($userid,$app_id){
		$this->load->database();
		$this->db->where('user_id',$userid);
		$this->db->where('id',$app_id);
		$query=$this->db->get('tbl_app');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $row){
				return $row->app_link;
			}
		}
		return $query->result();
	}

	function get_app($userid,$id){
		$this->load->database();
		$this->db->where('user_id',$userid);
		$this->db->where('id',$id);
		$query=$this->db->get('tbl_app');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function check_app_name($appname){
		$this->db->where('app_name',strtolower($appname));
		$query=$this->db->get('tbl_app');
		if ($query->num_rows() > 0)
		{
			return 1;
		}else{
			return 0;
		}
	}

	public function add_app($userid,$appname,$applink,$active){
		if($this->check_app_name($appname) == 0){
			$data = array(
					'user_id'=>$userid,
					'app_name'=>strtolower($appname),
					'app_link'=>$applink,
					'datecreated'=>date("Y-m-d h:s:m"),
					'active'=>$active
			);
			$this->db->insert('tbl_app', $data);
			return 1;
		} else {
			return 0;
		}
	}

	function update_app($appid,$userid,$appname,$applink,$active){
		$this->load->database();
		$data = array(
				'user_id'=>$userid,
				'app_name'=>strtolower($appname),
				'app_link'=>$applink,
				'active'=>$active,
				'dateupdated'=>date("Y-m-d h:s:m"),
		);
		$this->db->where('id', $appid);
		$this->db->update('tbl_app', $data);
	}

	function del_app($userid,$appid){
		$this->load->database();
		$this->db->where('user_id', $userid);
		$this->db->where('id', $appid);
		$this->db->delete('tbl_app');
	}

}